<?php

namespace app\admin\service;

use app\api\service\JinDie;
use app\common\model\tang\Cost;
use app\common\model\tang\CostAudit;
use app\common\model\tang\CostType;
use app\common\model\tang\Merchant;
use app\common\model\tang\Proof;
use think\Exception;
use think\facade\Log;
use app\common\model\tang\PayInfo;

/**
 * Class SystemLogService
 * @package admin\service
 */
class ProofService
{
    public static $type = [
        1 => '电费',
        2 => '充值记录',
        3 => '水费',
        4 => '余额转移',//1电费2充值3水费4余额转移1电费2充值3水费4余额转移5月结凭证6余额缴费7转账缴费8线上缴费9账单负数转余额
        5 => '月结凭证',
        6 => '余额缴费',
        7 => '转账缴费',
        8 => '线上缴费',
        9 => '账单负数转余额'
    ];
    public static $costTypeName = [
        11 => 'GAG维护费',
        4 => '花车管理费',
        8 => '比率推广费',
        10 => '仓库物业管理费',
        9 => '仓库租金',
        13 => '场地管理费',
        2 => '正柜浮动租金',
        1 => '正柜固定租金',
        12 => '广告位出租',
        7 => '固定推广费',
        5 => '商铺物业管理费',
        6 => '写字楼物业管理费',
        3 => '写字楼固定租金',
    ];

    /**
     * @param $data
     * @return 余额缴费凭证
     */
    public function payBalance($data)
    {
        $info = (new PayInfo())->whereBetweenTime('create_time', strtotime($data['start_time']), strtotime($data['end_time']) + 86400)->where('type', 2)->column('cost_id');
        $costIds = '';
        foreach ($info as $k => $row) {
            $costIds .= ',' . $row;
        }
        $costIds = trim($costIds, ',');
        if (empty($costIds)) {
            throw new Exception('该区间没有' . static::$type[$data['type']] . '凭证');
        }
        //根据支付情况寻找账单
//        dump($costIds);
        $costList = (new Cost())->with(['costType', 'merchant'])->whereIn('id', $costIds)->select()->toArray();
        $costModel = new CostType();
        $proof = [];
        $proof2 = [];
        $time = $data['end_time'];
        $all_money = 0;
        $bookDate = explode('-', $time);
        $round = strval(rand(0000, 9999));
        foreach ($costList as $ks => $v) {
            $costInfo = '';
            $costInfo = $costModel->where('id', $v['cost_type_id'])->find();
            $all_money += $v['all_amount'];

            //分三种情况  保证金类没有税
            if ($costInfo['tax_rate'] == 0) {
                $originalAmount = $v['all_amount'] / 100;
                $creditAmount = $v['all_amount'] / 100;
                $accountNumber = $costInfo['cost_sn'];
                $voucherAbstract = date('m.d', strtotime($v['pay_time'])) . '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . '余额转' . $costInfo['cost_name'];
                $asstActType1 = '客户';
                $asstActType2 = '合同';
                $asstActType3 = '品牌';
                $asstActType4 = '单元';
                $asstActType5 = '业态';
                $asstActNumber1 = $v['merchant']['merchant_sn'];
                $asstActName1 = $v['merchant']['tenant_name'];
                $asstActNumber2 = $v['merchant']['code'];
                $asstActName2 = $v['merchant']['tenant_name'];
                $asstActNumber3 = $v['merchant']['brand_code'];
                $asstActName3 = $v['merchant']['name'];
                $asstActNumber4 = '401001A' . $v['merchant']['positions_code'];
                $asstActName4 = $v['merchant']['positions_name'];
                $asstActNumber5 = $v['merchant']['bizType_code'];
                $asstActName5 = $v['merchant']['bizType_name'];
                $asstActType6 = '';
                $asstActNumber6 = '';
                $asstActName6 = '';
                $arr = [
                    "companyNumber" => '0109',
                    "bookedDate" => $time,//记账日期 充值则是 后推一天
                    "bizDate" => $time,//业务日期
                    "periodYear" => $bookDate[0],//会计期间-年
                    "periodNumber" => $bookDate[1],//会计期间-编码
                    "voucherType" => '转',//凭证字（凭证类型）
                    "attaches" => 1,//附件数量
                    "voucherNumber" => $round,//凭证号
                    "entrySeq" => count($proof) + 1, //分录行号
                    "voucherAbstract" => $voucherAbstract,
                    "accountNumber" => $accountNumber,
                    "currencyNumber" => "BB01",
                    "entryDC" => 0,
                    "originalAmount" => $originalAmount,
                    "debitAmount" => $creditAmount,
                    "creditAmount" => 0,
                    "creator" => "桂耀耀",
                    "asstActType1" => $asstActType1,
                    "asstActNumber1" => $asstActNumber1,
                    "asstActName1" => $asstActName1,
                    "asstActType2" => $asstActType2,
                    "asstActNumber2" => $asstActNumber2,
                    "asstActName2" => $asstActName2,
                    "asstActType3" => $asstActType3,
                    "asstActNumber3" => $asstActNumber3,
                    "asstActName3" => $asstActName3,
                    "asstActType4" => $asstActType4,
                    "asstActNumber4" => $asstActNumber4,
                    "asstActName4" => $asstActName4,
                    "asstActType5" => $asstActType5,
                    "asstActNumber5" => $asstActNumber5,
                    "asstActName5" => $asstActName5,
                    "asstActType6" => $asstActType6,
                    "asstActNumber6" => $asstActNumber6,
                    "asstActName6" => $asstActName6,
                    "asstActType7" => "",
                    "asstActNumber7" => "",
                    "asstActName7" => "",
                    "asstActType8" => "",
                    "asstActNumber8" => "",
                    "asstActName8" => ""
                ];
                $proof[] = $arr;
            } else {
                if ($v['cost_type_id'] == 46 && $v['is_admin_create'] == 1) {
                    $TaxMoney = TaxMoney($v['all_amount'] / 100, 1, $costInfo['tax_rate']);
                    for ($i = 0; $i < 5; $i++) {
                        if ($i == 0) {
                            $entryDC = 0;
                            $originalAmount = $v['all_amount'] / 100;
                            $creditAmount = 0;
                            $accountNumber = $costInfo['cost_sn'];
                            $debitAmount = $v['all_amount'] / 100;
                            $voucherAbstract = date('m.d', strtotime($v['pay_time'])) . '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . '余额转' . $costInfo['cost_name'];
                            $asstActType1 = '客户';
                            $asstActType2 = '合同';
                            $asstActType3 = '品牌';
                            $asstActType4 = '单元';
                            $asstActType5 = '业态';
                            $asstActNumber1 = $v['merchant']['merchant_sn'];
                            $asstActName1 = $v['merchant']['tenant_name'];
                            $asstActNumber2 = '99999999';
                            $asstActName2 = '默认合同';
                            $asstActNumber3 = '99999999';
                            $asstActName3 = '默认品牌';
                            $asstActNumber4 = '401001A' . $v['merchant']['positions_code'];
                            $asstActName4 = $v['merchant']['positions_code'];
                            $asstActNumber5 = '99999999';
                            $asstActName5 = '默认业态';
                            $asstActType6 = '';
                            $asstActNumber6 = '';
                            $asstActName6 = '';
                        }
                        if ($i == 1) {
                            $entryDC = 1;
                            $originalAmount = $v['all_amount'] / 100;
                            $creditAmount = $v['all_amount'] / 100;
                            $accountNumber = $costInfo['cost_sn'];
                            $debitAmount = 0;
                            $voucherAbstract = '结转' . $v['merchant']['company'] . '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . '装修水费';
                            $asstActNumber1 = $v['merchant']['merchant_sn'];
                            $asstActName1 = $v['merchant']['tenant_name'];
                            $asstActType1 = '客户';
                            $asstActType2 = '合同';
                            $asstActType3 = '品牌';
                            $asstActType4 = '单元';
                            $asstActType5 = '业态';
                            $asstActNumber2 = '99999999';
                            $asstActName2 = '默认合同';
                            $asstActNumber3 = '99999999';
                            $asstActName3 = '默认品牌';
                            $asstActNumber4 = '401001A' . $v['merchant']['positions_code'];
                            $asstActName4 = $v['merchant']['positions_code'];
                            $asstActNumber5 = '99999999';
                            $asstActName5 = '默认业态';
                            $asstActType6 = '';
                            $asstActNumber6 = '';
                            $asstActName6 = '';
                        }
                        //（140-4.08）/4.6*3.4
                        $chengben = round(($v['all_amount'] / 100 - $TaxMoney['tax_money']) / 4.6 * 3.4, 2);
                        if ($i == 2) {

                            $entryDC = 1;
                            $originalAmount = -$chengben;
                            $creditAmount = -$chengben;
                            $debitAmount = 0;
                            $accountNumber = $costInfo['taxation_sn'];
                            $voucherAbstract = '结转' . $v['merchant']['company'] . '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . '装修水费';
                            $asstActType1 = '客户';
                            $asstActType2 = '合同';
                            $asstActType3 = '单元';
                            $asstActType4 = '业态';
                            $asstActType5 = '成本中心-部门';
                            $asstActNumber1 = '99999999';
                            $asstActName1 = '默认客户';
                            $asstActNumber2 = '99999999';
                            $asstActName2 = '默认合同';
                            $asstActNumber3 = '99999999';
                            $asstActName3 = '默认单元';
                            $asstActNumber4 = '99999999';
                            $asstActName4 = '默认业态';
                            $asstActNumber5 = 'WYCB01';
                            $asstActName5 = '物业成本部';
                            $asstActType6 = '';
                            $asstActNumber6 = '';
                            $asstActName6 = '';
                        }

                        //（140-4.08-100.46）
                        $fuwu = bcsub(bcsub($v['all_amount'] / 100, $TaxMoney['tax_money'], 2), $chengben, 2);
                        if ($i == 3) {
                            $entryDC = 1;
                            $originalAmount = -$fuwu;
                            $creditAmount = -$fuwu;
                            $debitAmount = 0;
                            $accountNumber = '6401.03.01.02';
                            $voucherAbstract = '结转' . $v['merchant']['company'] . '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . '装修水费';
                            $asstActType1 = '';
                            $asstActType2 = '';
                            $asstActType3 = '';
                            $asstActType4 = '';
                            $asstActType5 = '';
                            $asstActNumber1 = '';
                            $asstActName1 = '';
                            $asstActNumber2 = '';
                            $asstActName2 = '';
                            $asstActNumber3 = '';
                            $asstActName3 = '';
                            $asstActNumber4 = '';
                            $asstActName4 = '';
                            $asstActNumber5 = '';
                            $asstActName5 = '';
                            $asstActType6 = '';
                            $asstActNumber6 = '';
                            $asstActName6 = '';
                        }
                        if ($i == 4) {
                            $entryDC = 1;
                            $originalAmount = $TaxMoney['tax_money'];
                            $debitAmount = $TaxMoney['tax_money'];
                            $creditAmount = 0;
                            $accountNumber = '6401.03.01.03';
                            $voucherAbstract = '结转' . $v['merchant']['company'] . '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . '装修水费应交税费' . '(' . intval($costInfo['tax_rate']) . '%)';
                            $asstActType1 = '客户';
                            $asstActType2 = '合同';
                            $asstActType3 = '品牌';
                            $asstActType4 = '单元';
                            $asstActType5 = '业态';
                            $asstActType6 = '税率';
                            $asstActNumber1 = '99999999';
                            $asstActName1 = '默认客户';
                            $asstActNumber2 = '99999999';
                            $asstActName2 = '默认合同';
                            $asstActNumber3 = '99999999';
                            $asstActName3 = '默认品牌';
                            $asstActNumber4 = '99999999';
                            $asstActName4 = '默认单元';
                            $asstActNumber5 = '99999999';
                            $asstActName5 = '默认业态';
                            $asstActNumber6 = strval(intval($costInfo['tax_rate']));
                            $asstActName6 = intval($costInfo['tax_rate']) . '%';
                        }
                        $arr = [
                            "companyNumber" => '0109',
                            "bookedDate" => $time,//记账日期 充值则是 后推一天
                            "bizDate" => $time,//业务日期
                            "periodYear" => $bookDate[0],//会计期间-年
                            "periodNumber" => $bookDate[1],//会计期间-编码
                            "voucherType" => '转',//凭证字（凭证类型）
                            "attaches" => 1,//附件数量
                            "voucherNumber" => $round,//凭证号
                            "entrySeq" => count($proof) + 1, //分录行号
                            "voucherAbstract" => $voucherAbstract,
                            "accountNumber" => $accountNumber,
                            "currencyNumber" => "BB01",
                            "entryDC" => $entryDC,
                            "originalAmount" => $originalAmount,
                            "debitAmount" => $debitAmount,
                            "creditAmount" => $creditAmount,
                            "creator" => "桂耀耀",
                            "asstActType1" => $asstActType1,
                            "asstActNumber1" => $asstActNumber1,
                            "asstActName1" => $asstActName1,
                            "asstActType2" => $asstActType2,
                            "asstActNumber2" => $asstActNumber2,
                            "asstActName2" => $asstActName2,
                            "asstActType3" => $asstActType3,
                            "asstActNumber3" => $asstActNumber3,
                            "asstActName3" => $asstActName3,
                            "asstActType4" => $asstActType4,
                            "asstActNumber4" => $asstActNumber4,
                            "asstActName4" => $asstActName4,
                            "asstActType5" => $asstActType5,
                            "asstActNumber5" => $asstActNumber5,
                            "asstActName5" => $asstActName5,
                            "asstActType6" => $asstActType6,
                            "asstActNumber6" => $asstActNumber6,
                            "asstActName6" => $asstActName6,
                            "asstActType7" => "",
                            "asstActNumber7" => "",
                            "asstActName7" => "",
                            "asstActType8" => "",
                            "asstActNumber8" => "",
                            "asstActName8" => ""
                        ];
                        $proof[] = $arr;
                    }


                } else {
                    $TaxMoney = TaxMoney($v['all_amount'] / 100, 1, $costInfo['tax_rate']);
                    for ($i = 0; $i < 2; $i++) {
                        if ($i == 0) {
                            $originalAmount = $TaxMoney['money'];
                            $creditAmount = $TaxMoney['money'];
                            $accountNumber = $costInfo['cost_sn'];
                            $voucherAbstract = date('m.d', strtotime($v['pay_time'])) . '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . '余额转' . $costInfo['cost_name'];
                            $asstActType1 = '客户';
                            $asstActType2 = '合同';
                            $asstActType3 = '品牌';
                            $asstActType4 = '单元';
                            $asstActType5 = '业态';
                            $asstActNumber1 = $v['merchant']['merchant_sn'];
                            $asstActName1 = $v['merchant']['tenant_name'];
                            $asstActNumber2 = $v['merchant']['code'];
                            $asstActName2 = $v['merchant']['tenant_name'];
                            $asstActNumber3 = $v['merchant']['brand_code'];
                            $asstActName3 = $v['merchant']['name'];
                            $asstActNumber4 = '401001A' . $v['merchant']['positions_code'];
                            $asstActName4 = $v['merchant']['positions_code'];
                            $asstActNumber5 = $v['merchant']['bizType_code'];
                            $asstActName5 = $v['merchant']['bizType_name'];
                            $asstActType6 = '';
                            $asstActNumber6 = '';
                            $asstActName6 = '';
//                            if($v['cost_type_id']==15){
//                                $asstActType3 = '单元';
//                                $asstActNumber3 = '401001A' . $v['merchant']['positions_code'];
//                                $asstActName3 = $v['merchant']['positions_code'];
//                                $asstActType4 = '业态';
//                                $asstActNumber4 = '99999999';
//                                $asstActName4 = '默认业态';
//                                $asstActType5 = '成本中心-部门';
//                                $asstActNumber5 ='YYSC01';
//                                $asstActName5 ='运营市场部';
//                            }
//                            if(in_array($v['cost_type_id'],[30,19,16,24,25,27,18,23,22,20,21]) ){
//                                $asstActType3 = '单元';
//                                $asstActNumber3 = '401001A' . $v['merchant']['positions_code'];
//                                $asstActName3 = $v['merchant']['positions_code'];
//                                $asstActType4 = '业态';
//                                $asstActNumber4 = '99999999';
//                                $asstActName4 = '默认业态';
//                                $asstActType5 = '成本中心-部门';
//                                $asstActNumber5 ='WYCB01';
//                                $asstActName5 ='物业成本部';
//                            }
                            if(in_array($v['cost_type_id'],[30,19,16,24,25,27,18,23,22,20,21,7,8,15,11]) ){
                                $asstActType3 = '单元';
                                $asstActNumber3 = '401001A' . $v['merchant']['positions_code'];
                                $asstActName3 = $v['merchant']['positions_code'];
                                $asstActType4 = '业态';
                                $asstActNumber4 = '99999999';
                                $asstActName4 = '默认业态';
                                $asstActType5 = '成本中心-部门';
                                $asstActNumber5 =$costInfo['cost_center_sn'];
                                $asstActName5 =$costInfo['cost_center'];
                            }
                        }
                        if ($i == 1) {
                            $originalAmount = $TaxMoney['tax_money'];
                            $creditAmount = $TaxMoney['tax_money'];
                            $accountNumber = $costInfo['taxation_sn'];
                            $voucherAbstract = date('m.d', strtotime($v['pay_time'])) . '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . '余额转' . $costInfo['cost_name'] . '应交税费(' . intval($costInfo['tax_rate']) . '%)';
                            $asstActType1 = '客户';
                            $asstActType2 = '合同';
                            $asstActType3 = '品牌';
                            $asstActType4 = '单元';
                            $asstActType5 = '业态';
                            $asstActNumber1 = '99999999';
                            $asstActName1 = '默认客户';
                            $asstActNumber2 = '99999999';
                            $asstActName2 = '默认合同';
                            $asstActNumber3 = '99999999';
                            $asstActName3 = '默认品牌';
                            $asstActNumber4 = '99999999';
                            $asstActName4 = '默认单元';
                            $asstActNumber5 = '99999999';
                            $asstActName5 = '默认业态';
                            $asstActType6 = '税率';
                            $asstActNumber6 = strval(intval($costInfo['tax_rate']));
                            $asstActName6 = intval($costInfo['tax_rate']) . '%';
                        }
                        $arr = [
                            "companyNumber" => '0109',
                            "bookedDate" => $time,//记账日期 充值则是 后推一天
                            "bizDate" => $time,//业务日期
                            "periodYear" => $bookDate[0],//会计期间-年
                            "periodNumber" => $bookDate[1],//会计期间-编码
                            "voucherType" => '转',//凭证字（凭证类型）
                            "attaches" => 1,//附件数量
                            "voucherNumber" => $round,//凭证号
                            "entrySeq" => count($proof) + 1, //分录行号
                            "voucherAbstract" => $voucherAbstract,
                            "accountNumber" => $accountNumber,
                            "currencyNumber" => "BB01",
                            "entryDC" => 0,
                            "originalAmount" => $originalAmount,
                            "debitAmount" => $creditAmount,
                            "creditAmount" => 0,
                            "creator" => "桂耀耀",
                            "asstActType1" => $asstActType1,
                            "asstActNumber1" => $asstActNumber1,
                            "asstActName1" => $asstActName1,
                            "asstActType2" => $asstActType2,
                            "asstActNumber2" => $asstActNumber2,
                            "asstActName2" => $asstActName2,
                            "asstActType3" => $asstActType3,
                            "asstActNumber3" => $asstActNumber3,
                            "asstActName3" => $asstActName3,
                            "asstActType4" => $asstActType4,
                            "asstActNumber4" => $asstActNumber4,
                            "asstActName4" => $asstActName4,
                            "asstActType5" => $asstActType5,
                            "asstActNumber5" => $asstActNumber5,
                            "asstActName5" => $asstActName5,
                            "asstActType6" => $asstActType6,
                            "asstActNumber6" => $asstActNumber6,
                            "asstActName6" => $asstActName6,
                            "asstActType7" => "",
                            "asstActNumber7" => "",
                            "asstActName7" => "",
                            "asstActType8" => "",
                            "asstActNumber8" => "",
                            "asstActName8" => ""
                        ];
                        $proof[] = $arr;
                    }
                }
            }
        }
        if ($all_money > 0) {

            $arr = [
                "companyNumber" => '0109',
                "bookedDate" => $time,//记账日期 充值则是 后推一天
                "bizDate" => $time,//业务日期
                "periodYear" => $bookDate[0],//会计期间-年
                "periodNumber" => $bookDate[1],//会计期间-编码
                "voucherType" => '转',//凭证字（凭证类型）
                "attaches" => 1,//附件数量
                "voucherNumber" => $round,//凭证号
                "entrySeq" => count($proof) + 1, //分录行号
                "voucherAbstract" => date('m.d', strtotime($costList[0]['pay_time'])) . '(' . $costList[0]['merchant']['positions_code'] . ',' . $costList[0]['merchant']['name'] . ')' . '余额转费用',
                "accountNumber" => '2205.27',
                "currencyNumber" => "BB01",
                "entryDC" => 0,
                "originalAmount" => $all_money / 100,
                "debitAmount" => 0,
                "creditAmount" => $all_money / 100,
                "creator" => "桂耀耀",
                "asstActType1" => "客户",
                "asstActNumber1" => $costList[0]['merchant']['merchant_sn'],
                "asstActName1" => $costList[0]['merchant']['tenant_name'],
                "asstActType2" => "合同",
                "asstActNumber2" => "99999999",
                "asstActName2" => "默认合同",
                "asstActType3" => "品牌",
                "asstActNumber3" => "99999999",
                "asstActName3" => "默认品牌",
                "asstActType4" => "单元",
                "asstActNumber4" => '401001A' . $costList[0]['merchant']['positions_code'],
                "asstActName4" => $costList[0]['merchant']['positions_code'],
                "asstActType5" => "业态",
                "asstActNumber5" => "99999999",
                "asstActName5" => "默认业态",
                "asstActType6" => "",
                "asstActNumber6" => "",
                "asstActName6" => "",
                "asstActType7" => "",
                "asstActNumber7" => "",
                "asstActName7" => "",
                "asstActType8" => "",
                "asstActNumber8" => "",
                "asstActName8" => ""
            ];
            $proof2[] = $arr;
        }
        $combine = array_merge($proof2, $proof);
        foreach ($combine as $key => $val) {
            $combine[$key]['entrySeq'] = $key + 1;
        }
        return $this->request_proof($data, $combine);
    }

    /**
     * @param $data
     * @return 线上缴费凭证
     */
    public function onlineBalance($data)
    {
        $daysection = (new PayInfo())
            ->whereBetweenTime('create_time', strtotime($data['start_time']), strtotime($data['end_time']) + 86400)
            ->where('type', 1)
            ->field("FROM_UNIXTIME(create_time,'%Y-%m-%d') as day")
            ->group('day')
            ->select()->toArray();
        if (empty($daysection)) {
            throw new Exception('该时间区间没有线上缴费凭证');
        }
        foreach ($daysection as $kes => $ves) {
            $infoList = (new PayInfo())
                ->where('type', 1)
                ->whereBetweenTime('create_time', strtotime($ves['day'] . ' 00:00:00'), strtotime($ves['day'] . ' 23:59:59'))
                ->column('cost_id');
            $all_money = 0;//总金额不含税
            $tax_money = 0;//总税额
            $order_money = 0;//多出的金额
            $costList = (new Cost())->with(['costType', 'merchant','sectionCost'])->whereIn('id', $infoList)->select()->toArray();
            $costModel = new CostType();
            $proof = [];
            $proof2 = [];
            $proof3 = [];
            $time = $ves['day'];
            $bookDate = explode('-', $time);
            $round = strval(rand(0000, 9999));
            foreach ($costList as $ks => $v) {
                if(isset($v['sectionCost'])&&!empty($v['sectionCost'])){
                    $v['pay_amount'] = $v['sectionCost']['amount']*100;
                    $v['all_amount'] = $v['pay_amount'];
                }
                if(empty($v['merchant']['name'])||$v['merchant']['name']=='-'){
                    $v['merchant']['name'] = $v['merchant']['tenant_name'];
                }
                $costInfo = '';
                $costInfo = $costModel->where('id', $v['cost_type_id'])->find();


                $all_money += $v['pay_amount'];
                $tax_money +=  round($v['pay_amount'] / 100*0.006,2);;
                //分三种情况  保证金类没有税
                if ($costInfo['tax_rate'] == 0) {

                    $originalAmount = $v['all_amount'] / 100;
                    $creditAmount = $v['all_amount'] / 100;
                    $accountNumber = $costInfo['cost_sn'];
                    $voucherAbstract = date('m.d', strtotime($ves['day'])) .'收'.$v['merchant']['name']. '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . $costInfo['cost_name'];
                    $asstActType1 = '客户';
                    $asstActType2 = '合同';
                    $asstActType3 = '品牌';
                    $asstActType4 = '单元';
                    $asstActType5 = '业态';
                    $asstActNumber1 = $v['merchant']['merchant_sn'];
                    $asstActName1 = $v['merchant']['tenant_name'];
                    $asstActNumber2 = $v['merchant']['code'];
                    $asstActName2 = $v['merchant']['tenant_name'];
                    $asstActNumber3 = $v['merchant']['brand_code'];
                    $asstActName3 = $v['merchant']['name'];
                    $asstActNumber4 = '401001A' . $v['merchant']['positions_code'];
                    $asstActName4 = $v['merchant']['positions_name'];
                    $asstActNumber5 = $v['merchant']['bizType_code'];
                    $asstActName5 = $v['merchant']['bizType_name'];
                    $asstActType6 = '';
                    $asstActNumber6 = '';
                    $asstActName6 = '';
                    $arr = [
                        "companyNumber" => '0109',
                        "bookedDate" => $time,//记账日期 充值则是 后推一天
                        "bizDate" => $time,//业务日期
                        "periodYear" => $bookDate[0],//会计期间-年
                        "periodNumber" => $bookDate[1],//会计期间-编码
                        "voucherType" => '转',//凭证字（凭证类型）
                        "attaches" => 1,//附件数量
                        "voucherNumber" => $round,//凭证号
                        "entrySeq" => count($proof) + 1, //分录行号
                        "voucherAbstract" => $voucherAbstract,
                        "accountNumber" => $accountNumber,
                        "currencyNumber" => "BB01",
                        "entryDC" => 0,
                        "originalAmount" => $originalAmount,
                        "debitAmount" => $creditAmount,
                        "creditAmount" => 0,
                        "creator" => "桂耀耀",
                        "asstActType1" => $asstActType1,
                        "asstActNumber1" => $asstActNumber1,
                        "asstActName1" => $asstActName1,
                        "asstActType2" => $asstActType2,
                        "asstActNumber2" => $asstActNumber2,
                        "asstActName2" => $asstActName2,
                        "asstActType3" => $asstActType3,
                        "asstActNumber3" => $asstActNumber3,
                        "asstActName3" => $asstActName3,
                        "asstActType4" => $asstActType4,
                        "asstActNumber4" => $asstActNumber4,
                        "asstActName4" => $asstActName4,
                        "asstActType5" => $asstActType5,
                        "asstActNumber5" => $asstActNumber5,
                        "asstActName5" => $asstActName5,
                        "asstActType6" => $asstActType6,
                        "asstActNumber6" => $asstActNumber6,
                        "asstActName6" => $asstActName6,
                        "asstActType7" => "",
                        "asstActNumber7" => "",
                        "asstActName7" => "",
                        "asstActType8" => "",
                        "asstActNumber8" => "",
                        "asstActName8" => ""
                    ];
                    $proof[] = $arr;
                } else {
                    if ($v['cost_type_id'] == 46 && $v['is_admin_create'] == 1) {
                        $TaxMoney = TaxMoney($v['all_amount'] / 100, 1, $costInfo['tax_rate']);

                        for ($i = 0; $i < 5; $i++) {
                            if ($i == 0) {
                                $entryDC = 0;
                                $originalAmount = $v['all_amount'] / 100;
                                $creditAmount = 0;
                                $accountNumber = $costInfo['cost_sn'];
                                $debitAmount = $v['all_amount'] / 100;
                                $voucherAbstract = date('m.d', strtotime($ves['day'])) .'收'.$v['merchant']['name']. '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . $costInfo['cost_name'];
                                $asstActType1 = '客户';
                                $asstActType2 = '合同';
                                $asstActType3 = '品牌';
                                $asstActType4 = '单元';
                                $asstActType5 = '业态';
                                $asstActNumber1 = $v['merchant']['merchant_sn'];
                                $asstActName1 = $v['merchant']['tenant_name'];
                                $asstActNumber2 = '99999999';
                                $asstActName2 = '默认合同';
                                $asstActNumber3 = '99999999';
                                $asstActName3 = '默认品牌';
                                $asstActNumber4 = '401001A' . $v['merchant']['positions_code'];
                                $asstActName4 = $v['merchant']['positions_code'];
                                $asstActNumber5 = '99999999';
                                $asstActName5 = '默认业态';
                                $asstActType6 = '';
                                $asstActNumber6 = '';
                                $asstActName6 = '';
                            }
                            if ($i == 1) {
                                $entryDC = 1;
                                $originalAmount = $v['all_amount'] / 100;
                                $creditAmount = $v['all_amount'] / 100;
                                $accountNumber = $costInfo['cost_sn'];
                                $debitAmount = 0;
                                $voucherAbstract = '结转' . $v['merchant']['company'] . '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . '装修水费';
                                $asstActNumber1 = $v['merchant']['merchant_sn'];
                                $asstActName1 = $v['merchant']['tenant_name'];
                                $asstActType1 = '客户';
                                $asstActType2 = '合同';
                                $asstActType3 = '品牌';
                                $asstActType4 = '单元';
                                $asstActType5 = '业态';
                                $asstActNumber2 = '99999999';
                                $asstActName2 = '默认合同';
                                $asstActNumber3 = '99999999';
                                $asstActName3 = '默认品牌';
                                $asstActNumber4 = '401001A' . $v['merchant']['positions_code'];
                                $asstActName4 = $v['merchant']['positions_code'];
                                $asstActNumber5 = '99999999';
                                $asstActName5 = '默认业态';
                                $asstActType6 = '';
                                $asstActNumber6 = '';
                                $asstActName6 = '';
                            }
                            //（140-4.08）/4.6*3.4
                            $chengben = round(($v['all_amount'] / 100 - $TaxMoney['tax_money']) / 4.6 * 3.4, 2);
                            if ($i == 2) {

                                $entryDC = 1;
                                $originalAmount = -$chengben;
                                $creditAmount = -$chengben;
                                $debitAmount = 0;
                                $accountNumber = $costInfo['taxation_sn'];
                                $voucherAbstract = '结转' . $v['merchant']['company'] . '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . '装修水费';
                                $asstActType1 = '客户';
                                $asstActType2 = '合同';
                                $asstActType3 = '单元';
                                $asstActType4 = '业态';
                                $asstActType5 = '成本中心-部门';
                                $asstActNumber1 = '99999999';
                                $asstActName1 = '默认客户';
                                $asstActNumber2 = '99999999';
                                $asstActName2 = '默认合同';
                                $asstActNumber3 = '99999999';
                                $asstActName3 = '默认单元';
                                $asstActNumber4 = '99999999';
                                $asstActName4 = '默认业态';
                                $asstActNumber5 = 'WYCB01';
                                $asstActName5 = '物业成本部';
                                $asstActType6 = '';
                                $asstActNumber6 = '';
                                $asstActName6 = '';
                            }

                            //（140-4.08-100.46）
                            $fuwu = bcsub(bcsub($v['all_amount'] / 100, $TaxMoney['tax_money'], 2), $chengben, 2);
                            if ($i == 3) {
                                $entryDC = 1;
                                $originalAmount = -$fuwu;
                                $creditAmount = -$fuwu;
                                $debitAmount = 0;
                                $accountNumber = '6401.03.01.02';
                                $voucherAbstract = '结转' . $v['merchant']['company'] . '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . '装修水费';
                                $asstActType1 = '';
                                $asstActType2 = '';
                                $asstActType3 = '';
                                $asstActType4 = '';
                                $asstActType5 = '';
                                $asstActNumber1 = '';
                                $asstActName1 = '';
                                $asstActNumber2 = '';
                                $asstActName2 = '';
                                $asstActNumber3 = '';
                                $asstActName3 = '';
                                $asstActNumber4 = '';
                                $asstActName4 = '';
                                $asstActNumber5 = '';
                                $asstActName5 = '';
                                $asstActType6 = '';
                                $asstActNumber6 = '';
                                $asstActName6 = '';
                            }
                            if ($i == 4) {
                                $entryDC = 1;
                                $originalAmount = $TaxMoney['tax_money'];
                                $debitAmount = $TaxMoney['tax_money'];
                                $creditAmount = 0;
                                $accountNumber = '6401.03.01.03';
                                $voucherAbstract = '结转' . $v['merchant']['company'] . '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . '装修水费应交税费' . '(' . intval($costInfo['tax_rate']) . '%)';
                                $asstActType1 = '客户';
                                $asstActType2 = '合同';
                                $asstActType3 = '品牌';
                                $asstActType4 = '单元';
                                $asstActType5 = '业态';
                                $asstActType6 = '税率';
                                $asstActNumber1 = '99999999';
                                $asstActName1 = '默认客户';
                                $asstActNumber2 = '99999999';
                                $asstActName2 = '默认合同';
                                $asstActNumber3 = '99999999';
                                $asstActName3 = '默认品牌';
                                $asstActNumber4 = '99999999';
                                $asstActName4 = '默认单元';
                                $asstActNumber5 = '99999999';
                                $asstActName5 = '默认业态';
                                $asstActNumber6 = strval(intval($costInfo['tax_rate']));
                                $asstActName6 = intval($costInfo['tax_rate']) . '%';
                            }
                            $arr = [
                                "companyNumber" => '0109',
                                "bookedDate" => $time,//记账日期 充值则是 后推一天
                                "bizDate" => $time,//业务日期
                                "periodYear" => $bookDate[0],//会计期间-年
                                "periodNumber" => $bookDate[1],//会计期间-编码
                                "voucherType" => '转',//凭证字（凭证类型）
                                "attaches" => 1,//附件数量
                                "voucherNumber" => $round,//凭证号
                                "entrySeq" => count($proof) + 1, //分录行号
                                "voucherAbstract" => $voucherAbstract,
                                "accountNumber" => $accountNumber,
                                "currencyNumber" => "BB01",
                                "entryDC" => $entryDC,
                                "originalAmount" => $originalAmount,
                                "debitAmount" => $debitAmount,
                                "creditAmount" => $creditAmount,
                                "creator" => "桂耀耀",
                                "asstActType1" => $asstActType1,
                                "asstActNumber1" => $asstActNumber1,
                                "asstActName1" => $asstActName1,
                                "asstActType2" => $asstActType2,
                                "asstActNumber2" => $asstActNumber2,
                                "asstActName2" => $asstActName2,
                                "asstActType3" => $asstActType3,
                                "asstActNumber3" => $asstActNumber3,
                                "asstActName3" => $asstActName3,
                                "asstActType4" => $asstActType4,
                                "asstActNumber4" => $asstActNumber4,
                                "asstActName4" => $asstActName4,
                                "asstActType5" => $asstActType5,
                                "asstActNumber5" => $asstActNumber5,
                                "asstActName5" => $asstActName5,
                                "asstActType6" => $asstActType6,
                                "asstActNumber6" => $asstActNumber6,
                                "asstActName6" => $asstActName6,
                                "asstActType7" => "",
                                "asstActNumber7" => "",
                                "asstActName7" => "",
                                "asstActType8" => "",
                                "asstActNumber8" => "",
                                "asstActName8" => ""
                            ];
                            $proof[] = $arr;
                        }


                    } else {
                        $TaxMoney = TaxMoney($v['all_amount'] / 100, 1, $costInfo['tax_rate']);
                        for ($i = 0; $i < 2; $i++) {
                            if ($i == 0) {
                                $originalAmount = $TaxMoney['money'];
                                $creditAmount = $TaxMoney['money'];
                                $accountNumber = $costInfo['cost_sn'];
                                $voucherAbstract = date('m.d', strtotime($ves['day'])) .'收'.$v['merchant']['name']. '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' .  $costInfo['cost_name'];
                                $asstActType1 = '客户';
                                $asstActType2 = '合同';
                                $asstActType3 = '品牌';
                                $asstActType4 = '单元';
                                $asstActType5 = '业态';
                                $asstActNumber1 = $v['merchant']['merchant_sn'];
                                $asstActName1 = $v['merchant']['tenant_name'];
                                $asstActNumber2 = $v['merchant']['code'];
                                $asstActName2 = $v['merchant']['tenant_name'];
                                $asstActNumber3 = $v['merchant']['brand_code'];
                                $asstActName3 = $v['merchant']['name'];
                                $asstActNumber4 = '401001A' . $v['merchant']['positions_code'];
                                $asstActName4 = $v['merchant']['positions_code'];
                                $asstActNumber5 = $v['merchant']['bizType_code'];
                                $asstActName5 = $v['merchant']['bizType_name'];
                                $asstActType6 = '';
                                $asstActNumber6 = '';
                                $asstActName6 = '';
                                if(in_array($v['cost_type_id'],[30,19,16,24,25,27,18,23,22,20,21,7,8,15,11]) ){
                                    $asstActType3 = '单元';
                                    $asstActNumber3 = '401001A' . $v['merchant']['positions_code'];
                                    $asstActName3 = $v['merchant']['positions_code'];
                                    $asstActType4 = '业态';
                                    $asstActNumber4 = '99999999';
                                    $asstActName4 = '默认业态';
                                    $asstActType5 = '成本中心-部门';
                                    $asstActNumber5 =$costInfo['cost_center_sn'];
                                    $asstActName5 =$costInfo['cost_center'];
                                }
//                                if($v['cost_type_id']==15){
//                                    $asstActType3 = '单元';
//                                    $asstActNumber3 = '401001A' . $v['merchant']['positions_code'];
//                                    $asstActName3 = $v['merchant']['positions_code'];
//                                    $asstActType4 = '业态';
//                                    $asstActNumber4 = '99999999';
//                                    $asstActName4 = '默认业态';
//                                    $asstActType5 = '成本中心-部门';
//                                    $asstActNumber5 ='YYSC01';
//                                    $asstActName5 ='运营市场部';
//                                }
//                                if(in_array($v['cost_type_id'],[30,19,16,24,25,27,18,23,22,20,21]) ){
//                                    $asstActType3 = '单元';
//                                    $asstActNumber3 = '401001A' . $v['merchant']['positions_code'];
//                                    $asstActName3 = $v['merchant']['positions_code'];
//                                    $asstActType4 = '业态';
//                                    $asstActNumber4 = '99999999';
//                                    $asstActName4 = '默认业态';
//                                    $asstActType5 = '成本中心-部门';
//                                    $asstActNumber5 ='WYCB01';
//                                    $asstActName5 ='物业成本部';
//                                }
                            }
                            if ($i == 1) {
                                $originalAmount = $TaxMoney['tax_money'];
                                $creditAmount = $TaxMoney['tax_money'];
                                $accountNumber = $costInfo['taxation_sn'];
                                $voucherAbstract = date('m.d', strtotime($ves['day'])) .'收'.$v['merchant']['name']. '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . $costInfo['cost_name'] . '应交税费(' . intval($costInfo['tax_rate']) . '%)';
                                $asstActType1 = '客户';
                                $asstActType2 = '合同';
                                $asstActType3 = '品牌';
                                $asstActType4 = '单元';
                                $asstActType5 = '业态';
                                $asstActNumber1 = '99999999';
                                $asstActName1 = '默认客户';
                                $asstActNumber2 = '99999999';
                                $asstActName2 = '默认合同';
                                $asstActNumber3 = '99999999';
                                $asstActName3 = '默认品牌';
                                $asstActNumber4 = '99999999';
                                $asstActName4 = '默认单元';
                                $asstActNumber5 = '99999999';
                                $asstActName5 = '默认业态';
                                $asstActType6 = '税率';
                                $asstActNumber6 = strval(intval($costInfo['tax_rate']));
                                $asstActName6 = intval($costInfo['tax_rate']) . '%';
                            }
                            $arr = [
                                "companyNumber" => '0109',
                                "bookedDate" => $time,//记账日期 充值则是 后推一天
                                "bizDate" => $time,//业务日期
                                "periodYear" => $bookDate[0],//会计期间-年
                                "periodNumber" => $bookDate[1],//会计期间-编码
                                "voucherType" => '转',//凭证字（凭证类型）
                                "attaches" => 1,//附件数量
                                "voucherNumber" => $round,//凭证号
                                "entrySeq" => count($proof) + 1, //分录行号
                                "voucherAbstract" => $voucherAbstract,
                                "accountNumber" => $accountNumber,
                                "currencyNumber" => "BB01",
                                "entryDC" => 0,
                                "originalAmount" => $originalAmount,
                                "debitAmount" => $creditAmount,
                                "creditAmount" => 0,
                                "creator" => "桂耀耀",
                                "asstActType1" => $asstActType1,
                                "asstActNumber1" => $asstActNumber1,
                                "asstActName1" => $asstActName1,
                                "asstActType2" => $asstActType2,
                                "asstActNumber2" => $asstActNumber2,
                                "asstActName2" => $asstActName2,
                                "asstActType3" => $asstActType3,
                                "asstActNumber3" => $asstActNumber3,
                                "asstActName3" => $asstActName3,
                                "asstActType4" => $asstActType4,
                                "asstActNumber4" => $asstActNumber4,
                                "asstActName4" => $asstActName4,
                                "asstActType5" => $asstActType5,
                                "asstActNumber5" => $asstActNumber5,
                                "asstActName5" => $asstActName5,
                                "asstActType6" => $asstActType6,
                                "asstActNumber6" => $asstActNumber6,
                                "asstActName6" => $asstActName6,
                                "asstActType7" => "",
                                "asstActNumber7" => "",
                                "asstActName7" => "",
                                "asstActType8" => "",
                                "asstActNumber8" => "",
                                "asstActName8" => ""
                            ];
                            $proof[] = $arr;
                        }
                    }
                }
            }
            if ($all_money > 0) {

                $arr = [
                    "companyNumber" => '0109',
                    "bookedDate" => $time,//记账日期 充值则是 后推一天
                    "bizDate" => $time,//业务日期
                    "periodYear" => $bookDate[0],//会计期间-年
                    "periodNumber" => $bookDate[1],//会计期间-编码
                    "voucherType" => '转',//凭证字（凭证类型）
                    "attaches" => 1,//附件数量
                    "voucherNumber" => $round,//凭证号
                    "entrySeq" => count($proof) + 1, //分录行号
                    "voucherAbstract" => date('m.d', strtotime($ves['day'])) .'收'.$v['merchant']['name']. '(' . $costList[0]['merchant']['positions_code'] . ',' . $costList[0]['merchant']['name'] . ')'.'应收款项(线上支付)',
                    "accountNumber" => '1122.18',
                    "currencyNumber" => "BB01",
                    "entryDC" => 1,
                    "originalAmount" => $all_money / 100,
                    "debitAmount" => 0,
                    "creditAmount" => $all_money / 100,
                    "creator" => "桂耀耀",
                    "asstActType1" => "",
                    "asstActNumber1" =>'',
                    "asstActName1" =>'',
                    "asstActType2" => "",
                    "asstActNumber2" => "",
                    "asstActName2" => "",
                    "asstActType3" => "",
                    "asstActNumber3" => "",
                    "asstActName3" => "",
                    "asstActType4" => "",
                    "asstActNumber4" => '',
                    "asstActName4" =>'',
                    "asstActType5" => "",
                    "asstActNumber5" => "",
                    "asstActName5" => "",
                    "asstActType6" => "",
                    "asstActNumber6" => "",
                    "asstActName6" => "",
                    "asstActType7" => "",
                    "asstActNumber7" => "",
                    "asstActName7" => "",
                    "asstActType8" => "",
                    "asstActNumber8" => "",
                    "asstActName8" => ""
                ];
                $arr1 = [
                    "companyNumber" => '0109',
                    "bookedDate" => $time,//记账日期 充值则是 后推一天
                    "bizDate" => $time,//业务日期
                    "periodYear" => $bookDate[0],//会计期间-年
                    "periodNumber" => $bookDate[1],//会计期间-编码
                    "voucherType" => '转',//凭证字（凭证类型）
                    "attaches" => 1,//附件数量
                    "voucherNumber" => $round,//凭证号
                    "entrySeq" => count($proof) + 1, //分录行号
                    "voucherAbstract" => date('m.d', strtotime($ves['day'])) .'收'.$v['merchant']['name']. '(' . $costList[0]['merchant']['positions_code'] . ',' . $costList[0]['merchant']['name'] . ')'.'应收款项(线上支付)手续费',
                    "accountNumber" => '6603.04',
                    "currencyNumber" => "BB01",
                    "entryDC" => 1,
                    "originalAmount" => $tax_money,
                    "debitAmount" => 0,
                    "creditAmount" => $tax_money,
                    "creator" => "桂耀耀",
                    "asstActType1" => "成本中心-部门",
                    "asstActNumber1" => "CW01",
                    "asstActName1" => "财务部",
                    "asstActType2" => "",
                    "asstActNumber2" => "",
                    "asstActName2" => "",
                    "asstActType3" => "",
                    "asstActNumber3" => "",
                    "asstActName3" => "",
                    "asstActType4" => "",
                    "asstActNumber4" => '',
                    "asstActName4" =>'',
                    "asstActType5" => "",
                    "asstActNumber5" => "",
                    "asstActName5" => "",
                    "asstActType6" => "",
                    "asstActNumber6" => "",
                    "asstActName6" => "",
                    "asstActType7" => "",
                    "asstActNumber7" => "",
                    "asstActName7" => "",
                    "asstActType8" => "",
                    "asstActNumber8" => "",
                    "asstActName8" => ""
                ];
                $proof2[] = $arr;
                $proof2[] =$arr1;
            }
            $combine = [];
            if ($order_money > 0) { //说明有多转账,需要充值余额,且增加一条凭证
                $order_array = [
                    "companyNumber" => '0109',
                    "bookedDate" => $time,//记账日期 充值则是 后推一天
                    "bizDate" => $time,//业务日期
                    "periodYear" => $bookDate[0],//会计期间-年
                    "periodNumber" => $bookDate[1],//会计期间-编码
                    "voucherType" => '收',//凭证字（凭证类型）
                    "attaches" => 1,//附件数量
                    "voucherNumber" => $round,//凭证号
                    "entrySeq" => count($proof) + 1, //分录行号
                    "voucherAbstract" => date('m.d', strtotime($ves['day'])) .'收'.$v['merchant']['name']. '(' . $costList[0]['merchant']['positions_code'] . ',' . $costList[0]['merchant']['name'] . ')' . '多付款',
                    "accountNumber" => '2205.27',
                    "currencyNumber" => "BB01",
                    "entryDC" => 0,
                    "originalAmount" => $order_money / 100,
                    "debitAmount" => 0,
                    "creditAmount" => $order_money / 100,
                    "creator" => "桂耀耀",
                    "asstActType1" => "客户",
                    "asstActNumber1" => $costList[0]['merchant']['merchant_sn'],
                    "asstActName1" =>  $costList[0]['merchant']['name'],
                    "asstActType2" => "合同",
                    "asstActNumber2" => "99999999",
                    "asstActName2" => "默认合同",
                    "asstActType3" => "品牌",
                    "asstActNumber3" => '99999999',
                    "asstActName3" => '默认品牌',
                    "asstActType4" => "单元",
                    "asstActNumber4" =>'401001A' . $costList[0]['merchant']['positions_code'],
                    "asstActName4" => $costList[0]['merchant']['positions_code'],
                    "asstActType5" => "业态",
                    "asstActNumber5" => "99999999",
                    "asstActName5" => "默认业态",
                    "asstActType6" => "",
                    "asstActNumber6" => "",
                    "asstActName6" => "",
                    "asstActType7" => "",
                    "asstActNumber7" => "",
                    "asstActName7" => "",
                    "asstActType8" => "",
                    "asstActNumber8" => "",
                    "asstActName8" => ""
                ];
                $proof3[]=$order_array;
            }
            $combine = array_merge($proof2, $proof);
            if(!empty($proof3)){
                $combine = array_merge($combine, $proof3);
            }
            foreach ($combine as $key => $val) {
                $combine[$key]['entrySeq'] = $key + 1;
            }
            $datas[] = $combine;


        }
        foreach ($datas as $k => $v) {
            $res = $this->request_proof($data, $v);
        }
        return $res;
    }

    /**
     * @param $data
     * @return 转账缴费凭证
     */
    public function transferBalance($data)
    {
        $info = (new CostAudit())->where('up_time','between',[$data['start_time'],$data['end_time']])->where('status', 1)->field('id,merchant_id,amount,in_amount,back_number,cost_id,up_time,back_number')->select()->toArray();
        if (empty($info)) {
            throw new Exception('该区间没有' . static::$type[$data['type']] . '凭证');
        }
        $costIds = '';
        foreach ($info as $k => $row) {
            //根据支付情况寻找账单
//        dump($costIds);
            $costList = (new Cost())->with(['costType', 'merchant'])->whereIn('id', $row['cost_id'])->select()->toArray();
            $costModel = new CostType();
            $proof = [];
            $proof2 = [];
            $proof3 = [];
            $time = $row['up_time'];
            $bookDate = explode('-', $time);
            $round = strval(rand(0000, 9999));
            foreach ($costList as $ks => $v) {
                $all_money = 0;
                $order_money = 0;
                $costInfo = '';
                $costInfo = $costModel->where('id', $v['cost_type_id'])->find();
                $all_money += $row['in_amount'];
                $order_money = bcsub($row['in_amount'], $row['amount']);
                if (empty($v['merchant']['name']) || $v['merchant']['name'] == '-') {
                    $v['merchant']['name'] = $v['merchant']['tenant_name'];
                }
                //分三种情况  保证金类没有税
                if ($costInfo['tax_rate'] == 0) {
                    $originalAmount = $v['pay_amount'] / 100;
                    $creditAmount = $v['pay_amount'] / 100;
                    $accountNumber = $costInfo['cost_sn'];
                    $voucherAbstract = date('m.d', strtotime($row['up_time'])) . '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . $costInfo['cost_name'];
                    $asstActType1 = '客户';
                    $asstActType2 = '合同';
                    $asstActType3 = '品牌';
                    $asstActType4 = '单元';
                    $asstActType5 = '业态';
                    $asstActNumber1 = $v['merchant']['merchant_sn'];
                    $asstActName1 = $v['merchant']['tenant_name'];
                    $asstActNumber2 = $v['merchant']['code'];
                    $asstActName2 = $v['merchant']['tenant_name'];
                    $asstActNumber3 = empty($v['merchant']['brand_code']) ? '99999999' : $v['merchant']['brand_code'];
                    $asstActName3 = $v['merchant']['name'];
                    $asstActNumber4 = '401001A' . $v['merchant']['positions_code'];
                    $asstActName4 = $v['merchant']['positions_name'];
                    $asstActNumber5 ='99999999';
                    $asstActName5 = '默认业态';
                    $asstActType6 = '';
                    $asstActNumber6 = '';
                    $asstActName6 = '';
                    $arr = [
                        "companyNumber" => '0109',
                        "bookedDate" => $time,//记账日期 充值则是 后推一天
                        "bizDate" => $time,//业务日期
                        "periodYear" => $bookDate[0],//会计期间-年
                        "periodNumber" => $bookDate[1],//会计期间-编码
                        "voucherType" => '收',//凭证字（凭证类型）
                        "attaches" => 1,//附件数量
                        "voucherNumber" => $round,//凭证号
                        "entrySeq" => count($proof) + 1, //分录行号
                        "voucherAbstract" => $voucherAbstract,
                        "accountNumber" => $accountNumber,
                        "currencyNumber" => "BB01",
                        "entryDC" => 0,
                        "originalAmount" => $originalAmount,
                        "debitAmount" => $creditAmount,
                        "creditAmount" => 0,
                        "creator" => "桂耀耀",
                        "asstActType1" => $asstActType1,
                        "asstActNumber1" => $asstActNumber1,
                        "asstActName1" => $asstActName1,
                        "asstActType2" => $asstActType2,
                        "asstActNumber2" => $asstActNumber2,
                        "asstActName2" => $asstActName2,
                        "asstActType3" => $asstActType3,
                        "asstActNumber3" => $asstActNumber3,
                        "asstActName3" => $asstActName3,
                        "asstActType4" => $asstActType4,
                        "asstActNumber4" => $asstActNumber4,
                        "asstActName4" => $asstActName4,
                        "asstActType5" => $asstActType5,
                        "asstActNumber5" => $asstActNumber5,
                        "asstActName5" => $asstActName5,
                        "asstActType6" => $asstActType6,
                        "asstActNumber6" => $asstActNumber6,
                        "asstActName6" => $asstActName6,
                        "asstActType7" => "",
                        "asstActNumber7" => "",
                        "asstActName7" => "",
                        "asstActType8" => "",
                        "asstActNumber8" => "",
                        "asstActName8" => ""
                    ];
                    $proof[] = $arr;
                } else {
                    if ($v['cost_type_id'] == 46 && $v['is_admin_create'] == 1) {
                        $TaxMoney = TaxMoney($v['all_amount'] / 100, 1, $costInfo['tax_rate']);
                        for ($i = 0; $i < 5; $i++) {
                            if ($i == 0) {
                                $entryDC = 0;
                                $originalAmount = $v['all_amount']/ 100;
                                $creditAmount = 0;
                                $accountNumber = $costInfo['cost_sn'];
                                $debitAmount = $v['all_amount']/ 100;
                                $voucherAbstract = date('m.d', strtotime($row['up_time'])) . '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . $costInfo['cost_name'];
                                $asstActType1 = '客户';
                                $asstActType2 = '合同';
                                $asstActType3 = '品牌';
                                $asstActType4 = '单元';
                                $asstActType5 = '业态';
                                $asstActNumber1 = $v['merchant']['merchant_sn'];
                                $asstActName1 = $v['merchant']['tenant_name'];
                                $asstActNumber2 = '99999999';
                                $asstActName2 = '默认合同';
                                $asstActNumber3 = '99999999';
                                $asstActName3 = '默认品牌';
                                $asstActNumber4 = '401001A' . $v['merchant']['positions_code'];
                                $asstActName4 = $v['merchant']['positions_code'];
                                $asstActNumber5 = '99999999';
                                $asstActName5 = '默认业态';
                                $asstActType6 = '';
                                $asstActNumber6 = '';
                                $asstActName6 = '';
                            }
                            if ($i == 1) {
                                $entryDC = 1;
                                $originalAmount = $v['all_amount'] / 100;
                                $creditAmount = $v['all_amount'] / 100;
                                $accountNumber = $costInfo['cost_sn'];
                                $debitAmount = 0;
                                $voucherAbstract = '结转' . $v['merchant']['company'] . '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . '装修水费';
                                $asstActNumber1 = $v['merchant']['merchant_sn'];
                                $asstActName1 = $v['merchant']['tenant_name'];
                                $asstActType1 = '客户';
                                $asstActType2 = '合同';
                                $asstActType3 = '品牌';
                                $asstActType4 = '单元';
                                $asstActType5 = '业态';
                                $asstActNumber2 = '99999999';
                                $asstActName2 = '默认合同';
                                $asstActNumber3 = '99999999';
                                $asstActName3 = '默认品牌';
                                $asstActNumber4 = '401001A' . $v['merchant']['positions_code'];
                                $asstActName4 = $v['merchant']['positions_code'];
                                $asstActNumber5 = '99999999';
                                $asstActName5 = '默认业态';
                                $asstActType6 = '';
                                $asstActNumber6 = '';
                                $asstActName6 = '';
                            }
                            //（140-4.08）/4.6*3.4
                            $chengben = round(($v['all_amount'] / 100 - $TaxMoney['tax_money']) / 4.6 * 3.4, 2);
                            if ($i == 2) {

                                $entryDC = 1;
                                $originalAmount = -$chengben;
                                $creditAmount = -$chengben;
                                $debitAmount = 0;
                                $accountNumber = $costInfo['taxation_sn'];
                                $voucherAbstract = '结转' . $v['merchant']['company'] . '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . '装修水费';
                                $asstActType1 = '客户';
                                $asstActType2 = '合同';
                                $asstActType3 = '单元';
                                $asstActType4 = '业态';
                                $asstActType5 = '成本中心-部门';
                                $asstActNumber1 = '99999999';
                                $asstActName1 = '默认客户';
                                $asstActNumber2 = '99999999';
                                $asstActName2 = '默认合同';
                                $asstActNumber3 = '99999999';
                                $asstActName3 = '默认单元';
                                $asstActNumber4 = '99999999';
                                $asstActName4 = '默认业态';
                                $asstActNumber5 = 'WYCB01';
                                $asstActName5 = '物业成本部';
                                $asstActType6 = '';
                                $asstActNumber6 = '';
                                $asstActName6 = '';
                            }

                            //（140-4.08-100.46）
                            $fuwu = bcsub(bcsub($v['all_amount'] / 100, $TaxMoney['tax_money'], 2), $chengben, 2);
                            if ($i == 3) {
                                $entryDC = 1;
                                $originalAmount = -$fuwu;
                                $creditAmount = -$fuwu;
                                $debitAmount = 0;
                                $accountNumber = '6401.03.01.02';
                                $voucherAbstract = '结转' . $v['merchant']['company'] . '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . '装修水费';
                                $asstActType1 = '';
                                $asstActType2 = '';
                                $asstActType3 = '';
                                $asstActType4 = '';
                                $asstActType5 = '';
                                $asstActNumber1 = '';
                                $asstActName1 = '';
                                $asstActNumber2 = '';
                                $asstActName2 = '';
                                $asstActNumber3 = '';
                                $asstActName3 = '';
                                $asstActNumber4 = '';
                                $asstActName4 = '';
                                $asstActNumber5 = '';
                                $asstActName5 = '';
                                $asstActType6 = '';
                                $asstActNumber6 = '';
                                $asstActName6 = '';
                            }
                            if ($i == 4) {
                                $entryDC = 1;
                                $originalAmount = $TaxMoney['tax_money'];
                                $debitAmount = $TaxMoney['tax_money'];
                                $creditAmount = 0;
                                $accountNumber = '6401.03.01.03';
                                $voucherAbstract = '结转' . $v['merchant']['company'] . '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . '装修水费应交税费' . '(' . intval($costInfo['tax_rate']) . '%)';
                                $asstActType1 = '客户';
                                $asstActType2 = '合同';
                                $asstActType3 = '品牌';
                                $asstActType4 = '单元';
                                $asstActType5 = '业态';
                                $asstActType6 = '税率';
                                $asstActNumber1 = '99999999';
                                $asstActName1 = '默认客户';
                                $asstActNumber2 = '99999999';
                                $asstActName2 = '默认合同';
                                $asstActNumber3 = '99999999';
                                $asstActName3 = '默认品牌';
                                $asstActNumber4 = '99999999';
                                $asstActName4 = '默认单元';
                                $asstActNumber5 = '99999999';
                                $asstActName5 = '默认业态';
                                $asstActNumber6 = strval(intval($costInfo['tax_rate']));
                                $asstActName6 = intval($costInfo['tax_rate']) . '%';
                            }
                            $arr = [
                                "companyNumber" => '0109',
                                "bookedDate" => $time,//记账日期 充值则是 后推一天
                                "bizDate" => $time,//业务日期
                                "periodYear" => $bookDate[0],//会计期间-年
                                "periodNumber" => $bookDate[1],//会计期间-编码
                                "voucherType" => '收',//凭证字（凭证类型）
                                "attaches" => 1,//附件数量
                                "voucherNumber" => $round,//凭证号
                                "entrySeq" => count($proof) + 1, //分录行号
                                "voucherAbstract" => $voucherAbstract,
                                "accountNumber" => $accountNumber,
                                "currencyNumber" => "BB01",
                                "entryDC" => $entryDC,
                                "originalAmount" => $originalAmount,
                                "debitAmount" => $debitAmount,
                                "creditAmount" => $creditAmount,
                                "creator" => "桂耀耀",
                                "asstActType1" => $asstActType1,
                                "asstActNumber1" => $asstActNumber1,
                                "asstActName1" => $asstActName1,
                                "asstActType2" => $asstActType2,
                                "asstActNumber2" => $asstActNumber2,
                                "asstActName2" => $asstActName2,
                                "asstActType3" => $asstActType3,
                                "asstActNumber3" => $asstActNumber3,
                                "asstActName3" => $asstActName3,
                                "asstActType4" => $asstActType4,
                                "asstActNumber4" => $asstActNumber4,
                                "asstActName4" => $asstActName4,
                                "asstActType5" => $asstActType5,
                                "asstActNumber5" => $asstActNumber5,
                                "asstActName5" => $asstActName5,
                                "asstActType6" => $asstActType6,
                                "asstActNumber6" => $asstActNumber6,
                                "asstActName6" => $asstActName6,
                                "asstActType7" => "",
                                "asstActNumber7" => "",
                                "asstActName7" => "",
                                "asstActType8" => "",
                                "asstActNumber8" => "",
                                "asstActName8" => ""
                            ];
                            $proof[] = $arr;
                        }
                    } else {
                        $TaxMoney = TaxMoney($v['all_amount']/ 100, 1, $costInfo['tax_rate']);

                        for ($i = 0; $i < 2; $i++) {
                            if ($i == 0) {
                                $originalAmount = $TaxMoney['money'];
                                $creditAmount = $TaxMoney['money'];
                                $accountNumber = $costInfo['cost_sn'];
                                $voucherAbstract = date('m.d', strtotime($row['up_time'])) . '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . $costInfo['cost_name'];
                                $asstActType1 = '客户';
                                $asstActType2 = '合同';
                                $asstActType3 = '品牌';
                                $asstActType4 = '单元';
                                $asstActType5 = '业态';
                                $asstActNumber1 = $v['merchant']['merchant_sn'];
                                $asstActName1 = $v['merchant']['tenant_name'];
                                $asstActNumber2 = $v['merchant']['code'];
                                $asstActName2 = $v['merchant']['tenant_name'];
                                $asstActNumber3 = empty($v['merchant']['brand_code']) ? '99999999' : $v['merchant']['brand_code'];
                                $asstActName3 = $v['merchant']['name'];
                                $asstActNumber4 = '401001A' . $v['merchant']['positions_code'];
                                $asstActName4 = $v['merchant']['positions_code'];
                                $asstActNumber5 ='99999999';
                                $asstActName5 ='默认业态';
                                $asstActType6 = '';
                                $asstActNumber6 = '';
                                $asstActName6 = '';
//                                if($v['cost_type_id']==15){
//                                    $asstActType3 = '单元';
//                                    $asstActNumber3 = '401001A' . $v['merchant']['positions_code'];
//                                    $asstActName3 = $v['merchant']['positions_code'];
//                                    $asstActType4 = '业态';
//                                    $asstActNumber4 = '99999999';
//                                    $asstActName4 = '默认业态';
//                                    $asstActType5 = '成本中心-部门';
//                                    $asstActNumber5 =$costInfo['cost_center_sn'];
//                                    $asstActName5 =$costInfo['cost_center'];
//                                }
//                                if($v['cost_type_id']==7||$v['cost_type_id']==8){
//                                    $asstActType3 = '单元';
//                                    $asstActNumber3 = '401001A' . $v['merchant']['positions_code'];
//                                    $asstActName3 = $v['merchant']['positions_code'];
//                                    $asstActType4 = '业态';
//                                    $asstActNumber4 = '99999999';
//                                    $asstActName4 = '默认业态';
//                                    $asstActType5 = '成本中心-部门';
//                                    $asstActNumber5 =$costInfo['cost_center_sn'];
//                                    $asstActName5 =$costInfo['cost_center'];
//                                }
                                if(in_array($v['cost_type_id'],[30,19,16,24,25,27,18,23,22,20,21,7,8,15,11]) ){
                                    $asstActType3 = '单元';
                                    $asstActNumber3 = '401001A' . $v['merchant']['positions_code'];
                                    $asstActName3 = $v['merchant']['positions_code'];
                                    $asstActType4 = '业态';
                                    $asstActNumber4 = '99999999';
                                    $asstActName4 = '默认业态';
                                    $asstActType5 = '成本中心-部门';
                                    $asstActNumber5 =$costInfo['cost_center_sn'];
                                    $asstActName5 =$costInfo['cost_center'];
                                }
                            }
                            if ($i == 1) {
                                $originalAmount = $TaxMoney['tax_money'];
                                $creditAmount = $TaxMoney['tax_money'];
                                $accountNumber = $costInfo['taxation_sn'];
                                $voucherAbstract = date('m.d', strtotime($row['up_time'])) . '(' . $v['merchant']['positions_code'] . ',' . $v['merchant']['name'] . ')' . $costInfo['cost_name'] . '应交税费(' . intval($costInfo['tax_rate']) . '%)';
                                $asstActType1 = '客户';
                                $asstActType2 = '合同';
                                $asstActType3 = '品牌';
                                $asstActType4 = '单元';
                                $asstActType5 = '业态';
                                $asstActNumber1 = '99999999';
                                $asstActName1 = '默认客户';
                                $asstActNumber2 = '99999999';
                                $asstActName2 = '默认合同';
                                $asstActNumber3 = '99999999';
                                $asstActName3 = '默认品牌';
                                $asstActNumber4 = '99999999';
                                $asstActName4 = '默认单元';
                                $asstActNumber5 = '99999999';
                                $asstActName5 = '默认业态';
                                $asstActType6 = '税率';
                                $asstActNumber6 = strval(intval($costInfo['tax_rate']));
                                $asstActName6 = intval($costInfo['tax_rate']) . '%';
                            }
                            $arr = [
                                "companyNumber" => '0109',
                                "bookedDate" => $time,//记账日期 充值则是 后推一天
                                "bizDate" => $time,//业务日期
                                "periodYear" => $bookDate[0],//会计期间-年
                                "periodNumber" => $bookDate[1],//会计期间-编码
                                "voucherType" => '收',//凭证字（凭证类型）
                                "attaches" => 1,//附件数量
                                "voucherNumber" => $round,//凭证号
                                "entrySeq" => count($proof) + 1, //分录行号
                                "voucherAbstract" => $voucherAbstract,
                                "accountNumber" => $accountNumber,
                                "currencyNumber" => "BB01",
                                "entryDC" => 0,
                                "originalAmount" => $originalAmount,
                                "debitAmount" => $creditAmount,
                                "creditAmount" => 0,
                                "creator" => "桂耀耀",
                                "asstActType1" => $asstActType1,
                                "asstActNumber1" => $asstActNumber1,
                                "asstActName1" => $asstActName1,
                                "asstActType2" => $asstActType2,
                                "asstActNumber2" => $asstActNumber2,
                                "asstActName2" => $asstActName2,
                                "asstActType3" => $asstActType3,
                                "asstActNumber3" => $asstActNumber3,
                                "asstActName3" => $asstActName3,
                                "asstActType4" => $asstActType4,
                                "asstActNumber4" => $asstActNumber4,
                                "asstActName4" => $asstActName4,
                                "asstActType5" => $asstActType5,
                                "asstActNumber5" => $asstActNumber5,
                                "asstActName5" => $asstActName5,
                                "asstActType6" => $asstActType6,
                                "asstActNumber6" => $asstActNumber6,
                                "asstActName6" => $asstActName6,
                                "asstActType7" => "",
                                "asstActNumber7" => "",
                                "asstActName7" => "",
                                "asstActType8" => "",
                                "asstActNumber8" => "",
                                "asstActName8" => ""
                            ];
                            $proof[] = $arr;
                        }
                    }
                }
            }
            if ($all_money > 0) {
                if (empty($costList[0]['merchant']['name']) || $costList[0]['merchant']['name'] == '-') {
                    $costList[0]['merchant']['name'] = $costList[0]['merchant']['tenant_name'];
                }

                if(strpos($row['back_number'],'8888')===false ){
                    $asstActNumber3 = '0005';
                    $asstActName3 = '交通银行繁华支行-6727';
                    $asstActNumber7 = '01.05';
                    $asstActName7 = '交通银行繁华支行';
                }else{

                    $asstActNumber3 = 'hf00005';
                    $asstActName3 = '兴业银行合肥胜利路支行-8888';
                    $asstActNumber7 = '13.01';
                    $asstActName7 = '兴业银行合肥胜利路支行';
                }

                $arr = [
                    "companyNumber" => '0109',
                    "bookedDate" => $time,//记账日期 充值则是 后推一天
                    "bizDate" => $time,//业务日期
                    "periodYear" => $bookDate[0],//会计期间-年
                    "periodNumber" => $bookDate[1],//会计期间-编码
                    "voucherType" => '收',//凭证字（凭证类型）
                    "attaches" => 1,//附件数量
                    "voucherNumber" => $round,//凭证号
                    "entrySeq" => count($proof) + 1, //分录行号
                    "voucherAbstract" => date('m.d', strtotime($row['up_time'])) . '(' . $costList[0]['merchant']['positions_code'] . ',' . $costList[0]['merchant']['name'] . ')' . '应收款项(转账)',
                    "accountNumber" => '1002.01',
                    "currencyNumber" => "BB01",
                    "entryDC" => 1,
                    "originalAmount" => $all_money / 100,
                    "debitAmount" => 0,
                    "creditAmount" => $all_money / 100,
                    "creator" => "桂耀耀",
                    "asstActType1" => "客户",
                    "asstActNumber1" =>'99999999',
                    "asstActName1" => '默认客户',
                    "asstActType2" => "合同",
                    "asstActNumber2" => "99999999",
                    "asstActName2" => "默认合同",
                    "asstActType3" => "银行账户",
                    "asstActNumber3" => $asstActNumber3,
                    "asstActName3" => $asstActName3,
                    "asstActType4" => "品牌",
                    "asstActNumber4" => '99999999',
                    "asstActName4" => '默认品牌',
                    "asstActType5" => "单元",
                    "asstActNumber5" => "99999999",
                    "asstActName5" => "默认单元",
                    "asstActType6" => "业态",
                    "asstActNumber6" => "99999999",
                    "asstActName6" => "默认业态",
                    "asstActType7" => "银行",
                    "asstActNumber7" => $asstActNumber7,
                    "asstActName7" => $asstActName7,
                    "asstActType8" => "",
                    "asstActNumber8" => "",
                    "asstActName8" => ""
                ];
                $proof2[] = $arr;
            }
            $combine = [];
            if ($order_money > 0) { //说明有多转账,需要充值余额,且增加一条凭证
                $order_array = [
                    "companyNumber" => '0109',
                    "bookedDate" => $time,//记账日期 充值则是 后推一天
                    "bizDate" => $time,//业务日期
                    "periodYear" => $bookDate[0],//会计期间-年
                    "periodNumber" => $bookDate[1],//会计期间-编码
                    "voucherType" => '收',//凭证字（凭证类型）
                    "attaches" => 1,//附件数量
                    "voucherNumber" => $round,//凭证号
                    "entrySeq" => count($proof) + 1, //分录行号
                    "voucherAbstract" => date('m.d', strtotime($row['up_time'])) . '(' . $costList[0]['merchant']['positions_code'] . ',' . $costList[0]['merchant']['name'] . ')' . '多付款',
                    "accountNumber" => '2205.27',
                    "currencyNumber" => "BB01",
                    "entryDC" => 0,
                    "originalAmount" => $order_money / 100,
                    "debitAmount" => 0,
                    "creditAmount" => $order_money / 100,
                    "creator" => "桂耀耀",
                    "asstActType1" => "客户",
                    "asstActNumber1" => $costList[0]['merchant']['merchant_sn'],
                    "asstActName1" =>  $costList[0]['merchant']['name'],
                    "asstActType2" => "合同",
                    "asstActNumber2" => "99999999",
                    "asstActName2" => "默认合同",
                    "asstActType3" => "品牌",
                    "asstActNumber3" => '99999999',
                    "asstActName3" => '默认品牌',
                    "asstActType4" => "单元",
                    "asstActNumber4" =>'401001A' . $costList[0]['merchant']['positions_code'],
                    "asstActName4" => $costList[0]['merchant']['positions_code'],
                    "asstActType5" => "业态",
                    "asstActNumber5" => "99999999",
                    "asstActName5" => "默认业态",
                    "asstActType6" => "",
                    "asstActNumber6" => "",
                    "asstActName6" => "",
                    "asstActType7" => "",
                    "asstActNumber7" => "",
                    "asstActName7" => "",
                    "asstActType8" => "",
                    "asstActNumber8" => "",
                    "asstActName8" => ""
                ];
                $proof3[]=$order_array;
            }
            $combine = array_merge($proof2, $proof);
            if(!empty($proof3)){
                $combine = array_merge($combine, $proof3);
            }
            foreach ($combine as $key => $val) {
                $combine[$key]['entrySeq'] = $key + 1;
            }
            $datas[] = $combine;
        }
        foreach ($datas as $k => $v) {
            $res = $this->request_proof($data, $v);
        }

        return $res;
    }

    /**
     * @param $data推送月结凭证
     * @return mixed
     */
    public function carryOver($data)
    {

        $lastDay = date('Y-m-t', strtotime($data['start_time']));
        Log::info('--------推送月结账单开始----------');
        $costInfo = (new Cost())->with(['merchant', 'costType'])
            ->whereNotIn('merchant_id', [222, 1916, 1917, 1918])
            ->where('start_time', '>=', strtotime($data['start_time'] . ' 00:00:00'))
//            ->where('start_time', '<', strtotime($lastDay . ' 23:59:59'))
            ->where('end_time', '<=', strtotime($lastDay . ' 23:59:59'))
            ->where(['cost_type_id' => $data['cost_type_id']])
            ->select()->toArray();
        if (empty($costInfo)) {
            throw new Exception('所属账期没有此月结凭证');
        }
        $voucherNumber = strval(rand(0001, 9999));
        $time = explode('-', $data['start_time']);
        $first_day = $data['start_time'] . '-01';
        $lsatDay = date('Y-m-d', strtotime("$first_day +1month -1 day"));

        $proof = [];
        foreach ($costInfo as $k => $v) {
            $brand = '';
            if (!empty($v['merchant']['name']) && $v['merchant']['name'] != '-') {
                $brand = '(' . $v['merchant']['name'] . ')';
            }
            $brand = str_replace('&', '-', $brand);

            $allMoney = TaxMoney($v['all_amount'] / 100, 1, $v['costType']['tax_rate']);
            $arr = [
                "companyNumber" => '0109',
                "bookedDate" => $lsatDay,//记账日期 充值则是 后推一天
                "bizDate" => $lsatDay,//业务日期
                "periodYear" => $time[0],//会计期间-年
                "periodNumber" => $time[1],//会计期间-编码
                "voucherType" => '转',//凭证字（凭证类型）
                "attaches" => 1,//附件数量
                "voucherNumber" => $voucherNumber,//凭证号
                "entrySeq" => $k + 1, //分录行号
                "voucherAbstract" => "应收" . $v['merchant']['tenant_name'] . ($v['merchant']['positions_code'] . ',' . $brand) . $time[0] . '年' . $time[1] . '月' . static::$costTypeName[$data['cost_type_id']] . '_预收',
                "accountNumber" => $v['costType']['cost_sn'],
                "currencyNumber" => "BB01",
                "entryDC" => 1,
                "originalAmount" => $allMoney['money'],
                "debitAmount" => $allMoney['money'],
                "creditAmount" => 0,
                "creator" => "桂耀耀",
                "asstActType1" => "客户",
                "asstActNumber1" => $v['merchant']['merchant_sn'],
                "asstActName1" => $v['merchant']['tenant_name'],
                "asstActType2" => "合同",
                "asstActNumber2" => $v['merchant']['code'],
                "asstActName2" => $brand,
                "asstActType3" => "品牌",
                "asstActNumber3" => empty($v['merchant']['brand_code']) ? '99999999' : $v['merchant']['brand_code'],
                "asstActName3" => $brand,
                "asstActType4" => "单元",
                "asstActNumber4" => '401001A' . $v['merchant']['positions_code'],
                "asstActName4" => '401001A' . $v['merchant']['positions_name'],
                "asstActType5" => "业态",
                "asstActNumber5" =>'99999999',
                "asstActName5" =>'默认业态',
                "asstActType6" => "",
                "asstActNumber6" => "",
                "asstActName6" => "",
                "asstActType7" => "",
                "asstActNumber7" => "",
                "asstActName7" => "",
                "asstActType8" => "",
                "asstActNumber8" => "",
                "asstActName8" => ""
            ];

            $arr1 = [
                "companyNumber" => '0109',
                "bookedDate" => $lsatDay,//记账日期 充值则是 后推一天
                "bizDate" => $lsatDay,//业务日期
                "periodYear" => $time[0],//会计期间-年
                "periodNumber" => $time[1],//会计期间-编码
                "voucherType" => '转',//凭证字（凭证类型）
                "attaches" => 1,//附件数量
                "voucherNumber" => $voucherNumber,//凭证号
                "entrySeq" => $k + 1, //分录行号
                "voucherAbstract" => "确认" . $v['merchant']['tenant_name'] . ($v['merchant']['positions_code'] . ',' . $brand) . $time[0] . '年' . $time[1] . '月' . static::$costTypeName[$data['cost_type_id']] . '_预收_收入',
                "accountNumber" => $v['costType']['carry_down_sn'],
                "currencyNumber" => "BB01",
                "entryDC" => 0,
                "originalAmount" => $allMoney['money'],
                "debitAmount" => 0,
                "creditAmount" => $allMoney['money'],
                "creator" => "桂耀耀",
                "asstActType1" => "客户",
                "asstActNumber1" => $v['merchant']['merchant_sn'],
                "asstActName1" => $v['merchant']['tenant_name'],
                "asstActType2" => "合同",
                "asstActNumber2" => $v['merchant']['code'],
                "asstActName2" => $brand,
                "asstActType3" => "单元",
                "asstActNumber3" => '401001A' . $v['merchant']['positions_code'],
                "asstActName3" => $v['merchant']['positions_name'],
                "asstActType4" => "业态",
                "asstActNumber4" => '99999999',
                "asstActName4" => '默认业态',
                "asstActType5" => "成本中心-部门",
                "asstActNumber5" =>  $v['costType']['cost_center_sn'],
                "asstActName5" => $v['costType']['cost_center'],
                "asstActType6" => "",
                "asstActNumber6" => "",
                "asstActName6" => "",
                "asstActType7" => "",
                "asstActNumber7" => "",
                "asstActName7" => "",
                "asstActType8" => "",
                "asstActNumber8" => "",
                "asstActName8" => ""
            ];
            $proof[] = $arr;
            $proof1[] = $arr1;
        }
        $proof = array_merge($proof, $proof1);
        foreach ($proof as $ks => $vs) {
            $proof[$ks]['entrySeq'] = $ks + 1;
        }
        Log::info('--------推送月结费用开始----------' . json_encode($proof));
        return $this->request($data, $proof);
    }

    public function request_proof($data, $proof)
    {
        $jin = new JinDie();
        $proofModel = new Proof();
        $res = $jin->importVoucher($proof);
        if (empty($res)) {
            throw new Exception('金蝶接口异常');
        }
        unset($data['__token__']);
        $proofData['info'] = $res['message'];
        $proofData['create_time'] = time();
        $proofData['update_time'] = time();
        $proofData['type'] = $data['type'];
        $proofData['push'] = '推送' . $data['start_time'] . static::$type[$data['type']] . '凭证';
        $proofModel->save($proofData);
        return $res;
    }

    public function request($data, $proof)
    {
        $jin = new JinDie();
        $proofModel = new Proof();
        $res = $jin->importVoucher($proof);
        if (empty($res)) {
            throw new Exception('金蝶接口异常');
        }
        unset($data['__token__']);
        $proofData['info'] = $res['message'];
        $proofData['create_time'] = time();
        $proofData['update_time'] = time();
        $proofData['type'] = $data['type'];
        if (isset($data['cost_type_id']) && !empty($data['cost_type_id'])) {
            $proofData['push'] = '推送' . $data['start_time'] . static::$type[$data['type']] . (static::$costTypeName[$data['cost_type_id']]) . '凭证';
        } else {
            $proofData['push'] = '推送' . $data['start_time'] . '负数账单充值余额凭证';
        }
        $proofModel->save($proofData);
        return $res;
    }

    public function Balance($merchantId, $proofData)
    {
        if (!empty($proofData)) {
            Log::info('--------负数账单充值余额开始----------' . json_encode($proofData));
        }
        $merInfo = (new Merchant())->where('id', $merchantId)->find();
        $costModel = new CostType();
        $proof = [];
        $voucherNumber = strval(rand(0001, 9999));
        $time = date('Y-m-d');
        $bookDate = explode('-', $time);
        foreach ($proofData as $k => $v) {
            $costInfo = '';
            $costInfo = $costModel->where('id', $v['cost_type_id'])->find();
            $TaxMoney = TaxMoney($v['amount'], 1, $costInfo['tax_rate']);
            for ($i = 0; $i < 2; $i++) {
                if ($i == 0) {
                    $originalAmount = $TaxMoney['money'];
                    $creditAmount = $TaxMoney['money'];
                    $accountNumber = $costInfo['cost_sn'];
                    $voucherAbstract = $merInfo['positions_code'] . $merInfo['company'] . $merInfo['name'] . $costInfo['cost_name'] . '负数账单充值';
                    $asstActNumber1 = $merInfo['merchant_sn'];
                    $asstActName1 = $merInfo['tenant_name'];
                    $asstActNumber2 = $merInfo['code'];
                    $asstActName2 = $merInfo['tenant_name'];
                    if ($merInfo['brand_code']) {
                        $asstActNumber3 = $merInfo['brand_code'];
                        $asstActName3 = $merInfo['name'];
                    } else {
                        $asstActNumber3 = '99999999';
                        $asstActName3 = '默认品牌';
                    }
                    $asstActNumber4 = '401001A' . $merInfo['positions_code'];
                    $asstActName4 = $merInfo['positions_name'];
//                    if ($merInfo['bizType_code']) {
//                        $asstActNumber5 = $merInfo['bizType_code'];
//                        $asstActName5 = $merInfo['bizType_name'];
//                    } else {
                        $asstActNumber5 = '99999999';
                        $asstActName5 = '默认业态';
//                    }
                    $asstActType6 = '';
                    $asstActNumber6 = '';
                    $asstActName6 = '';
                }
                if ($i == 1) {
                    $originalAmount = $TaxMoney['tax_money'];
                    $creditAmount = $TaxMoney['tax_money'];
                    $accountNumber = $costInfo['taxation_sn'];
                    $voucherAbstract = $merInfo['positions_code'] . $merInfo['company'] . $merInfo['name'] . $costInfo['cost_name'] . '负数账单充值应交税费(' . intval($costInfo['tax_rate']) . '%)';
                    $asstActNumber1 = '99999999';
                    $asstActName1 = '默认客户';
                    $asstActNumber2 = '99999999';
                    $asstActName2 = '默认合同';
                    $asstActNumber3 = '99999999';
                    $asstActName3 = '默认品牌';
                    $asstActNumber4 = '99999999';
                    $asstActName4 = '默认单元';
                    $asstActNumber5 = '99999999';
                    $asstActName5 = '默认业态';
                    $asstActType6 = '税率';
                    $asstActNumber6 = strval(intval($costInfo['tax_rate']));
                    $asstActName6 = intval($costInfo['tax_rate']) . '%';
                }
                $arr = [
                    "companyNumber" => '0109',
                    "bookedDate" => $time,//记账日期 充值则是 后推一天
                    "bizDate" => $time,//业务日期
                    "periodYear" => $bookDate[0],//会计期间-年
                    "periodNumber" => $bookDate[1],//会计期间-编码
                    "voucherType" => '收',//凭证字（凭证类型）
                    "attaches" => 1,//附件数量
                    "voucherNumber" => $voucherNumber,//凭证号
                    "entrySeq" => count($proof) + 1, //分录行号
                    "voucherAbstract" => $voucherAbstract,
                    "accountNumber" => $accountNumber,
                    "currencyNumber" => "BB01",
                    "entryDC" => -1,
                    "originalAmount" => $originalAmount,
                    "debitAmount" => 0,
                    "creditAmount" => $creditAmount,
                    "creator" => "桂耀耀",
                    "asstActType1" => "客户",
                    "asstActNumber1" => $asstActNumber1,
                    "asstActName1" => $asstActName1,
                    "asstActType2" => "合同",
                    "asstActNumber2" => $asstActNumber2,
                    "asstActName2" => $asstActName2,
                    "asstActType3" => "品牌",
                    "asstActNumber3" => $asstActNumber3,
                    "asstActName3" => $asstActName3,
                    "asstActType4" => "单元",
                    "asstActNumber4" => $asstActNumber4,
                    "asstActName4" => $asstActName4,
                    "asstActType5" => "业态",
                    "asstActNumber5" => $asstActNumber5,
                    "asstActName5" => $asstActName5,
                    "asstActType6" => $asstActType6,
                    "asstActNumber6" => $asstActNumber6,
                    "asstActName6" => $asstActName6,
                    "asstActType7" => "",
                    "asstActNumber7" => "",
                    "asstActName7" => "",
                    "asstActType8" => "",
                    "asstActNumber8" => "",
                    "asstActName8" => ""
                ];
                $proof[] = $arr;
            }
        }
        $sell = count($proof) / 2;
        for ($s = 0; $s < $sell; $s++) {
            $costInfo = $costModel->where('id', $proofData[$s]['cost_type_id'])->find();
            $zheng = [
                "companyNumber" => '0109',
                "bookedDate" => $time,//记账日期 充值则是 后推一天
                "bizDate" => $time,//业务日期
                "periodYear" => $bookDate[0],//会计期间-年
                "periodNumber" => $bookDate[1],//会计期间-编码
                "voucherType" => '收',//凭证字（凭证类型）
                "attaches" => 1,//附件数量
                "voucherNumber" => $voucherNumber,//凭证号
                "entrySeq" => count($proof) + 1, //分录行号
                "voucherAbstract" => $merInfo['positions_code'] . $merInfo['company'] . $merInfo['name'] . $costInfo['cost_name'] . '负数账单充值',
                "accountNumber" => '2205.27',
                "currencyNumber" => "BB01",
                "entryDC" => -1,
                "originalAmount" => -$proofData[$s]['amount'],
                "debitAmount" => 0,
                "creditAmount" => -$proofData[$s]['amount'],
                "creator" => "桂耀耀",
                "asstActType1" => "客户",
                "asstActNumber1" => $merInfo['merchant_sn'],
                "asstActName1" => $merInfo['tenant_name'],
                "asstActType2" => "合同",
                "asstActNumber2" => '99999999',
                "asstActName2" => '默认合同',
                "asstActType3" => "品牌",
                "asstActNumber3" => '99999999',
                "asstActName3" => '默认品牌',
                "asstActType4" => "单元",
                "asstActNumber4" => '401001A' . $merInfo['positions_code'],
                "asstActName4" => '401001A' . $merInfo['positions_name'],
                "asstActType5" => "业态",
                "asstActNumber5" => '99999999',
                "asstActName5" => '默认业态',
                "asstActType6" => '',
                "asstActNumber6" => '',
                "asstActName6" => '',
                "asstActType7" => "",
                "asstActNumber7" => "",
                "asstActName7" => "",
                "asstActType8" => "",
                "asstActNumber8" => "",
                "asstActName8" => ""
            ];
            $proof[] = $zheng;
        }
        $data = [
            'start_time' => $time,
            'type' => 9,
        ];
        Log::info('--------负数账单充值余额参数----------' . json_encode($proof));
        return $this->request($data, $proof);
    }


}